package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.DistributeLine;
import com.zyd.shiro.business.service.PakDistributeLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakAssets;
import com.zyd.shiro.persistence.beans.PakDistributeLine;
import com.zyd.shiro.persistence.mapper.PakAssetsMapper;
import com.zyd.shiro.persistence.mapper.PakDistributeLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakDistributeLineServiceImpl implements PakDistributeLineService {

    @Autowired
    private PakDistributeLineMapper pakDistributeLineMapper;

    @Autowired
    private PakAssetsMapper pakAssetsMapper;

    @Override
    public DistributeLine insert(DistributeLine distributeLine) {
        Assert.notNull(distributeLine, "对象不可为空！");
        distributeLine.getPakDistributeLine().setUpdateTime(new Date());
        distributeLine.getPakDistributeLine().setCreateTime(new Date());
        pakDistributeLineMapper.insertSelective(distributeLine.getPakDistributeLine());
        return distributeLine;
    }

    @Override
    public void insertList(List<DistributeLine> distributeLines) {
            for(DistributeLine distributeLine:distributeLines){
               distributeLine.getPakDistributeLine().setUpdateTime(new Date());
               distributeLine.getPakDistributeLine().setCreateTime(new Date());
               pakDistributeLineMapper.insertSelective(distributeLine.getPakDistributeLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakDistributeLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(DistributeLine distributeLine) {
        Assert.notNull(distributeLine, "对象不可为空！");
        distributeLine.setUpdateTime(new Date());
        return pakDistributeLineMapper.updateByPrimaryKey(distributeLine.getPakDistributeLine()) > 0;
    }

    @Override
    public boolean updateSelective(DistributeLine distributeLine) {
        Assert.notNull(distributeLine, "对象不可为空！");
        distributeLine.setUpdateTime(new Date());
        return pakDistributeLineMapper.updateByPrimaryKeySelective(distributeLine.getPakDistributeLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public DistributeLine getByPrimaryKey(Long primaryKey) {
        PakDistributeLine pakDistributeLine = pakDistributeLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakDistributeLine ? null : new DistributeLine(pakDistributeLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public DistributeLine getOneByEntity(DistributeLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakDistributeLine pakDistributeLine = pakDistributeLineMapper.selectOne(test.getPakDistributeLine());
        return null == pakDistributeLine ? null : new DistributeLine(pakDistributeLine);
    }

    @Override
    public List<DistributeLine> listAll() {
        List<PakDistributeLine> pakDistributeLines = pakDistributeLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakDistributeLines)) {
            return null;
        }
        List<DistributeLine> distributeLines = new ArrayList<>();
        for (PakDistributeLine pakDistributeLine : pakDistributeLines) {
            distributeLines.add(new DistributeLine(pakDistributeLine));
        }
        return distributeLines;
    }

    @Override
    public List<DistributeLine> listByEntity(DistributeLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<DistributeLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakDistributeLine> pakDistributeLines = pakDistributeLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakDistributeLines)) {
            return null;
        }
        List<DistributeLine> distributeLines = new ArrayList<>();
        for (PakDistributeLine pakDistributeLine : pakDistributeLines) {
            distributeLines.add(new DistributeLine(pakDistributeLine));
        }
        PageInfo bean = new PageInfo<PakDistributeLine>(pakDistributeLines);
        bean.setList(distributeLines);
        return bean;
    }
    @Override
    public void increase(PakDistributeLine pakDistributeLine) {
        pakDistributeLineMapper.insert(pakDistributeLine);
    }

    @Override
    public List<DistributeLine> findByDistribute(Long distributeId) {
        List<PakDistributeLine> pakDistributeLines = pakDistributeLineMapper.findByDistribute(distributeId);
        if (CollectionUtils.isEmpty(pakDistributeLines)) {
            return null;
        }
        List<DistributeLine> distributeLines = new ArrayList<>();
        for (PakDistributeLine pakDistributeLine : pakDistributeLines) {
            PakAssets pakAssets = pakAssetsMapper.selectByPrimaryKey(pakDistributeLine.getAssets());
            distributeLines.add(new DistributeLine(pakDistributeLine,pakAssets));
        }
        return distributeLines;
    }


}
